<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <#include "/common/base.html" >
    <script>

    </script>
</head>
<body class="easyui-layout">
    <div data-options="region:'center',fit: true">
        <table id="user-datagrid"></table>
    </div>
</body>
<script>
    var $USER_GRID = null;
    $(function () {
        $USER_GRID = $('#user-datagrid');
        initDataGrid();
    })
    function initDataGrid(){
        $USER_GRID.datagrid({
            url: '/users/list',
            fit: true,
            fitColumns: true,
            border: false,
            striped: true,
            idField: 'id',
            loadMsg: '加载中，请稍候',
            rownumbers: true,
            toolbar: [{
                iconCls: 'icon-add',
                text: '增加',
                handler: function(){
                    openWindow('添加用户', '/users/add_index');
                }
            },'-',{
                iconCls: 'icon-edit',
                text: '修改',
                handler: function(){
                    var secRow = $USER_GRID.datagrid('getSelections');
                    if(secRow.length != 1){
                        msg('只能选择1行！');return;
                    }
                    openWindow('添加用户', '/users/update_index?ids='+secRow[0]['id']);
                }
            },'-',{
                iconCls: 'icon-remove',
                text: '删除',
                handler: function(){
                    var ids = [];
                    var secRows = $USER_GRID.datagrid('getSelections');
                    if(secRows.length < 1){
                        msg('至少选择1行！');return;
                    }
                    secRows.forEach(function (a,b,c) {
                        ids.push(a['id']);
                    })
                    confirmDelete(ids);
                }
            },'-',{
                iconCls: 'icon-man',
                text: '授权',
                handler: function(){
                    var id = -1;
                    var secRows = $USER_GRID.datagrid('getSelections');
                    if(secRows.length != 1){
                        msg('请选择1行！');return;
                    }
                    secRows.forEach(function (a,b,c) {
                        id = a['id'];
                    })
                    openWindow('授予角色', '/users/auth_index?id='+id, {width: 680});
                }
            }],
            pagination: true,
            pageNumber: 1,
            pageSize: 10,
            columns: [[
                {field: 'id', checkbox: true, width: 20},
                {title: '姓名', field: 'name', width: 100},
                {title: '年龄', field: 'age', width: 100},
                {title: '生日', field: 'birth', width: 100, formatter: function (value,row,index) {
                        var date = new Date(value);
                        return date.format("yyyy-MM-dd hh:mm:ss");
                    }},
                {title: '注册时间', field: 'createTime', width: 100, formatter: function (value,row,index) {
                        var date = new Date(value);
                        return date.format("yyyy-MM-dd hh:mm:ss");
                    }}
            ]]
            // ,
            // data: [
            //     {id: '1', name:'张三', age:'12', birth: '2018-02-05', createTime: '2015-06-29 15:25:56'},
            //     {id: '2', name:'李四', age:'13', birth: '2018-01-05', createTime: '2015-05-29 15:25:56'},
            //     {id: '3', name:'王八', age:'14', birth: '2018-12-05', createTime: '2015-04-29 15:25:56'}
            // ]
        })
    }
    function dataReload(){
        $USER_GRID.datagrid('reload');
    }
    function confirmDelete(ids){
        art.dialog({
            content: '确定要删除吗？',
            ok: function () {
                $.ajax({
                    url: 'delete',
                    type: 'post',
                    data: {idStr: ids.join(',')},
                    success: function(rsp){
                        if(rsp['success']){
                            msg('删除成功', true);
                            closeReload();
                        }else {
                            msg(rsp['info'],true);
                        }
                    },
                    error: function(){msg('删除出错',true);}
                });
            },
            cancelVal: '关闭',
            cancel: true //为true等价于function(){}
        });
    }
    function getSelections(){
        var secRows = $USER_GRID.datagrid('getSelections');
        return secRows;
    }
</script>
</html>